package com.hhs.storage.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hhs.storage.bean.PersonConfig;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.person.config.PersonPlanUndoNotifyConfig;
import com.hhs.storage.dao.PersonConfigDao;
import com.hhs.storage.service.PersonConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-04-21
 */
@Service
@Transactional
public class PersonConfigServiceImpl extends ServiceImpl<PersonConfigDao, PersonConfig> implements PersonConfigService {
    @Resource
    private HttpServletRequest request;
    /**
     * 获取-个人计划未执行通知设置
     * @param userId
     * @return
     */
    @Override
    public Map getPersonPlanUndoNotifyConfig(String userId) {
        //获取用户id
        userId=getUserId(userId);
        PersonConfig config=this.getByType(PersonConfig.TYPE_PERSON_PLAN_UNDO_NOTIFY,userId);
        if (config==null){
            //数据库中没有保存，则设置默认值
            return ResponseInfo.success(new PersonPlanUndoNotifyConfig());
        }
        String json=config.getData();
        //转换成对象
        PersonPlanUndoNotifyConfig data= JSON.parseObject(json,PersonPlanUndoNotifyConfig.class);
        return ResponseInfo.success(data);
    }
    /**
     * 更新-个人计划未执行通知设置
     * @param userId
     * @param open
     * @param time
     * @return
     */
    @Override
    public Map editPersonPlanUndoNotifyConfig(String userId, Boolean open, Integer time) {
        if (time<=0){
            return ResponseInfo.error("时间间隔必须大于0");
        }
        //获取用户id
        userId=getUserId(userId);
        //参数设置
        PersonPlanUndoNotifyConfig json=new PersonPlanUndoNotifyConfig();
        json.setOpen(open);
        json.setTime(time);
        String data=JSON.toJSONString(json);

        this.saveOrUpdate(PersonConfig.TYPE_PERSON_PLAN_UNDO_NOTIFY,userId,data);
        return ResponseInfo.success();
    }
    /**
     * 添加-统计忽略人员设置
     * @return
     */
    @Override
    public Map getPlanStatisticalIgnoreUserConfig() {
        PersonConfig config=this.getByType(PersonConfig.TYPE_PLAN_STATISTICAL_IGNORE_USER,null);
        if (config==null){
            //数据库中没有保存，则设置默认值
            return ResponseInfo.success(new String[]{});
        }
        String json=config.getData();
        return ResponseInfo.success(json.split(","));
    }
    /**
     * 更新-统计忽略人员设置
     * @param userIds 忽略的人员id
     * @return
     */
    @Override
    public Map editPlanStatisticalIgnoreUserConfig(String userIds) {
        this.saveOrUpdate(PersonConfig.TYPE_PLAN_STATISTICAL_IGNORE_USER,null,userIds);
        return ResponseInfo.success();
    }

    /**
     * 根据类型获取配置信息
     * @param type
     * @param userId
     */
    @Override
    public PersonConfig getByType(String type,String userId){
        QueryWrapper qw=new QueryWrapper<PersonConfig>();
        if(StringUtils.isNotEmpty(userId)){
            qw.eq("user_id",userId);
        }
        qw.eq("type",type);
        List<PersonConfig> list=this.list(qw);
        if (list.size()==0){
            return null;
        }
        return  list.get(0);
    }

    @Override
    public Map editSysLayout(String configStr) {
        //获取用户id
        String userId= SysUtil.getLoginUserId(request);
        if(!StringUtils.isEmpty(userId)){
            this.saveOrUpdate(PersonConfig.TYPE_SYS_LAYOUT,userId,configStr);
        }
        return ResponseInfo.success();
    }

    /**
     * 新增更新配置
     * @param type
     * @param userId
     * @param data
     */
    public void saveOrUpdate(String type,String userId,String data){
        QueryWrapper qw=new QueryWrapper<PersonConfig>();
        if(StringUtils.isNotEmpty(userId)){
            qw.eq("user_id",userId);
        }
        qw.eq("type",type);

        List<PersonConfig> list=this.list(qw);
        if (list.size()==0){//新增
            PersonConfig config=new PersonConfig();
            config.setId(SysUtil.UUID());
            if(StringUtils.isNotEmpty(userId)){
                config.setUserId(userId);
            }
            config.setType(type);
            config.setData(data);
            this.save(config);
        }else{
            UpdateWrapper<PersonConfig> uw=new UpdateWrapper<>();
            //更新
            PersonConfig config=list.get(0);
            uw.eq("id",config.getId());
            uw.set("data",data);
            this.update(uw);
        }
    }
    /**
     * 获取用户id
     * @param userId
     * @return
     */
    public String getUserId(String userId){
        if (StringUtils.isEmpty(userId)){
            userId= SysUtil.getLoginUserId(request);
        }
        return  userId;
    }
}
